//
//  LSZClassificationDetailsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/27.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZClassificationDetailsVC.h"

@interface LSZClassificationDetailsVC ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property(nonatomic,strong) UITableView * tableViewNoticeDetails;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,assign) NSInteger indexNum;//记录长按删除的是那个
@property(nonatomic,strong) NSMutableArray * arrayDataClass;
@end

@implementation LSZClassificationDetailsVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableViewNoticeDetails reloadData];
}
-(void)getClassificationData :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic detailsofthenotificationinterface:weakSelf.type kind:weakSelf.kind tag:weakSelf.tag page:[NSString stringWithFormat:@"%ld",(long)page] :^(BOOL isOk, NSString * string, LSZObtainNotificationDetailsManager * manager) {
        if(isOk == YES ){
            if (page == 1) {
                [weakSelf.arrayDataClass removeAllObjects];
                [weakSelf.arrayDataClass addObjectsFromArray:manager.arrayData];
                if (manager.arrayData.count == 0) {
                    weakSelf.tableViewNoticeDetails.mj_footer.hidden = YES;
                   LSZEmptyDataVC * emptyDataVC = [[LSZEmptyDataVC alloc]init];
                    [emptyDataVC showWithView:weakSelf.tableViewNoticeDetails];
                }
            }else{
                if (manager.arrayData.count == 0) {
                    [weakSelf.tableViewNoticeDetails.mj_footer endRefreshingWithNoMoreData];
                }
                [weakSelf.arrayDataClass addObjectsFromArray:manager.arrayData];
            }
            [self.tableViewNoticeDetails reloadData];
        }else{
            [self initWithtring:string];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self.view tapGesture:^(UIGestureRecognizer *ges) {
         
    }];
    self.arrayDataClass = [[NSMutableArray alloc]init];
    [self getClassificationData:self.page];
    [self.tableViewNoticeDetails registerClass:[LSZNotifyBasicInformationTableViewCell class] forCellReuseIdentifier:@"LSZNotifyBasicInformationTableViewCell"];
    [self.tableViewNoticeDetails registerClass:[LSZNotificationOriginalInformationTableViewCell class] forCellReuseIdentifier:@"LSZNotificationOriginalInformationTableViewCell"];
    self.tableViewNoticeDetails.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getClassificationData :self.page];
            [self.tableViewNoticeDetails.mj_header endRefreshing];
        });
    }];
    self.tableViewNoticeDetails.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getClassificationData:self.page];
            [self.tableViewNoticeDetails.mj_footer endRefreshing];
        });
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(allHaveBeenReadNot) name:@"allHaveBeenRead" object:nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrayDataClass.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CGFloat height = [LSZSpecialPackaging initWithReturnsthesizeOf:30 :5];
        CGFloat heightLab = [LSZSpecialPackaging initWithReturnsthesizeOf:18 :2];
        return 30-2+height+heightLab;
    }else{
        CGFloat height = [LSZSpecialPackaging initWithReturnsthesizeOf:50 :5];
        return height+20;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZObtainNotificationDetailsMg * mg = nil;
    mg = self.arrayDataClass[indexPath.section];
    if (indexPath.row == 0) {
        LSZNotifyBasicInformationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZNotifyBasicInformationTableViewCell" forIndexPath:indexPath];
        [cell.imageAvatar sd_setImageWithURL:[NSURL URLWithString:mg.avatar] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.labelName.text = mg.username;
        cell.labelTime.text = mg.add_time;
        if([self.tag isEqualToString:@"4,5"]){
            if(mg.comment_content.length != 0){
                cell.labelInteractiveClasses.text = [NSString stringWithFormat:@"回复我:%@",mg.from_comment_content];//对评论的回复
            }else{
                cell.labelInteractiveClasses.text = mg.from_comment_content;//对动态的回复
            }
        }else{
            if(mg.comment_content.length != 0){
                cell.labelInteractiveClasses.text = @"赞了您的评论";
            }else{
                cell.labelInteractiveClasses.text = @"赞了您的动态";
            }
        }
        if ([mg.is_read isEqualToString:@"0"]) {
            cell.viewRed.hidden = NO;
        }else{
            cell.viewRed.hidden = YES;
        }
        UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)];
        //设定最小的长按时间 按不够这个时间不响应手势
        longPressGR.minimumPressDuration = 0.7;
        [cell addGestureRecognizer:longPressGR];

        [cell tapGesture:^(UIGestureRecognizer *ges) {
            NSLog(@"单击");
            [self clickOnThatArea:indexPath];
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZNotificationOriginalInformationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZNotificationOriginalInformationTableViewCell" forIndexPath:indexPath];
        if(mg.comment_content.length != 0){
            cell.labelContent.text = [NSString stringWithFormat:@"我的评论:%@",mg.comment_content];
        }else{
            cell.labelContent.text = [NSString stringWithFormat:@"我的动态:%@",mg.dynamic_content];
        }
        UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)];
        //设定最小的长按时间 按不够这个时间不响应手势
        longPressGR.minimumPressDuration = 0.7;
        [cell addGestureRecognizer:longPressGR];

        [cell tapGesture:^(UIGestureRecognizer *ges) {
            NSLog(@"单击");
            [self clickOnThatArea:indexPath];
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self clickOnThatArea:indexPath];
}
-(void)clickOnThatArea :(NSIndexPath *)indexPath
{
    if (self.arrayDataClass.count > indexPath.section) {
        LSZObtainNotificationDetailsMg * mg = nil;
        mg = self.arrayDataClass[indexPath.section];
        [LSZRequestResolutionDic allDynamicLists:mg.kind id_id:mg.tid type:self.type fuid:@"" selfStr:@"" theme:@"" page:@"1" is_city:@"" is_follow:@"" hot:@"" :^(BOOL isOk, NSString * string, LSZAllDynamicListsManager * manager) {
            if (isOk == YES) {
                if ([mg.is_read isEqualToString:@"0"]) {
                    [LSZRequestData readTheInformationInTheNotice:@"2" strID:mg.id_id :^(BOOL isOK, NSString * string, NSDictionary * diction) {
                        if (isOK == YES) {
                            mg.is_read = @"1";
                            [_UserM setMsg_not_read:[NSString stringWithFormat:@"%ld",(long)[[_UserM msg_not_read] integerValue]-1]];
                        }
                    }];
                }
                LSZAllDynamicListsMg * AllDynamicListsMg = nil;
                AllDynamicListsMg = manager.arrayData[0];
                LSZDynamicCommentDetailsVC * vc = [[LSZDynamicCommentDetailsVC alloc]init];
                vc.AllDynamicListsMg = AllDynamicListsMg;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }];
    }
}
#pragma mark -实现手势对应的功能
-(void)lpGR:(UILongPressGestureRecognizer *)lpGR
{
    if (lpGR.state == UIGestureRecognizerStateBegan) {//手势开始
        CGPoint point = [lpGR locationInView:self.tableViewNoticeDetails];
        NSIndexPath *indexPath =  [self.tableViewNoticeDetails indexPathForRowAtPoint:point];
        if (indexPath != nil){
            self.indexNum = indexPath.section;
        }
    }
    if (lpGR.state == UIGestureRecognizerStateEnded){
        //手势结束
        [self longPressDelete];//添加弹窗
    }
}
#pragma mark - 删除通知
-(void)longPressDelete
{
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"删除"] actionSheetBlock:^(NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            LSZObtainNotificationDetailsMg * mg = nil;
            mg = self.arrayDataClass[self.indexNum];

            [LSZRequestData deleteTheNotice:mg.id_id :^(BOOL isOk, NSString * string, NSDictionary * diction) {
                if (isOk == YES) {
                    [self initWithtring:@"删除成功!"];
                    [self.arrayDataClass removeObjectAtIndex:self.indexNum];
                    [self.tableViewNoticeDetails reloadData];
                }else{
                    [self initWithtring:string];
                }
            }];

        }
    }];
    [actionSheet show];
}
-(void)allHaveBeenReadNot
{
    for (int i = 0; i < self.arrayDataClass.count; i++) {
        LSZObtainNotificationDetailsMg * mg = nil;
        mg = self.arrayDataClass[i];
        if ([mg.is_read isEqualToString:@"0"]) {
            mg.is_read = @"1";
        }
    }
    [self.tableViewNoticeDetails reloadData];
}
-(UITableView *)tableViewNoticeDetails
{
    if (_tableViewNoticeDetails == nil) {
        _tableViewNoticeDetails = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-44-NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableViewNoticeDetails.delegate = self;
        _tableViewNoticeDetails.dataSource = self;
        _tableViewNoticeDetails.separatorColor = divider;
        _tableViewNoticeDetails.estimatedRowHeight =0;
        _tableViewNoticeDetails.estimatedSectionFooterHeight =0;
        _tableViewNoticeDetails.estimatedSectionHeaderHeight =0;
        _tableViewNoticeDetails.separatorInset = UIEdgeInsetsMake(0, 20, 20, 20);
        [self.view addSubview:_tableViewNoticeDetails];
    }
    return _tableViewNoticeDetails;
}

@end
